#include<iostream>
#include<string>
#include<pthread.h>
#include <unistd.h>

pthread_cond_t gcond=PTHREAD_COND_INITIALIZER;//定义条件变量
pthread_mutex_t gmutex=PTHREAD_MUTEX_INITIALIZER;//锁
void* active(void* args)
{
    while(1)
    {
        pthread_mutex_lock(&gmutex);
        pthread_cond_wait(&gcond,&gmutex);//锁内等待
        std::cout<<(const char*)args<<" active!"<<std::endl;//加锁保护显示器文件

        pthread_mutex_unlock(&gmutex);
    }
    return (void*)0;
}
int main()
{
    pthread_t t1,t2,t3,t4;
    pthread_create(&t1,nullptr,active,(void*)"thread-1");
    pthread_create(&t2,nullptr,active,(void*)"thread-2");
    pthread_create(&t3,nullptr,active,(void*)"thread-3");
    pthread_create(&t4,nullptr,active,(void*)"thread-4");

    sleep(5);
    
    while(1)
    {
        pthread_cond_signal(&gcond);
        //pthread_cond_broadcast
        sleep(1);
    }

    pthread_join(t1,nullptr);
    pthread_join(t2,nullptr);
    pthread_join(t3,nullptr);
    pthread_join(t4,nullptr);

    return 0;
}